package com.atguigu.daijia.driver.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @create: 2024/10/1
 * @author: gyhe10
 * @description: 自定义线程池
 **/

@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        // 动态获取服务器核心数
        int corePoolSize = Runtime.getRuntime().availableProcessors();
        return new ThreadPoolExecutor(
                // 核心线程数，IO密集型：2-3倍CPU核数，计算密集型：CPU核数+1
                corePoolSize + 1,
                // 最大线程数
                corePoolSize + 1,
                // 线程空闲超过多少秒就销毁
                10,
                TimeUnit.SECONDS,
                // 阻塞队列
                new ArrayBlockingQueue<>(100),
                // 拒绝策略，直接拒绝
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
